package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.service.VideoService;
import com.engineering.elink.util.ResJsonUtil;

@RestController
@CrossOrigin
@RequestMapping("/api/video")
@Api(value="视频接口")
public class VideoController {

	@Autowired
	private VideoService videoService;
	
	/**
	 * 最近浏览视频
	 */
	@RequestMapping(value="/getBrowseVideo",method=RequestMethod.GET)
	@ApiOperation(value="最近浏览视频")
	public Map<String,Object> getBrowseVideo(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		if(StringUtils.isNotEmpty(userId)){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,videoService.getBrowseVideo(co));
		}
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}
	
	/**
	 * 
	 * 默认播放课程下第一章第一节第一个视频(支待免费和收费)
	 * 收费可看所有,则免费状态不传入，免费只允许看免费的免费状态传0
	 */
	@RequestMapping(value="/getStudyVideo",method=RequestMethod.GET)
	@ApiOperation(value="默认播放课程下第一章第一节第一个视频")
	public Map<String,Object> getStudyVideo(
			@ApiParam(value="课程id",required=true) @RequestParam(value = "courseId",required=true)Integer courseId,
			@ApiParam(value="视频免费状态(0免费1收费)",required=false,defaultValue="0") @RequestParam(value = "freeStatus",required=false,defaultValue="0")Integer freeStatus
			,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
		}
		
		return videoService.selectDefaultOneVideo(courseId,freeStatus,userId);
		
	}
	
	/**
	 * 单个视频
	 */
	@RequestMapping(value="/get",method = RequestMethod.GET)
	@ApiOperation(value = "单个视频")
	public Map<String, Object> get(
			@ApiParam(value="视频id",required=true) @RequestParam(value = "videoId",required=true)Integer videoId,
			HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		
		return videoService.get(videoId,userInfo);
	}
	
	/**
	 * 获取下一个视频
	 */
	@RequestMapping(value="/getNextVideo",method=RequestMethod.GET)
	@ApiOperation(value="获取下一个视频")
	public Map<String,Object> getNextVideo(
			@ApiParam(value="课程id",required=true) @RequestParam(value = "courseId",required=true)Integer courseId,
			@ApiParam(value="视频免费状态(0免费1收费)",required=false) @RequestParam(value = "freeStatus",required=false)Integer freeStatus,
			@ApiParam(value="当前视频id",required=true) @RequestParam(value = "videoId",required=true)Integer videoId
			,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,videoService.selectNextVideo(courseId, freeStatus, videoId,userId));
		
	}
	
	/**
	 * 相关视频
	 */
	@RequestMapping(value="/getSameVideo",method=RequestMethod.GET)
	@ApiOperation(value="相关视频")
	public Map<String,Object> getSameVideo(
			@ApiParam(value="当前视频id",required=false) @RequestParam(value = "videoId",required=false)Integer videoId){
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,videoService.getSameVideo(null,videoId));
	}
	
	@RequestMapping(value="/syncVideo",method=RequestMethod.POST)
	@ApiOperation(value="同步视频")
	public Map<String,Object> syncVideo(
			@ApiParam(value="云资源地址",required=true) @RequestParam(value = "cloudAddress",required=true)String cloudAddress,
			@ApiParam(value="用户seed",required=true) @RequestParam(value="seed",required=true)String seed, 
			@ApiParam(value="用户authToken",required=true)@RequestParam(value="authToken",required=true)String authToken,
			@ApiParam(value="视频对象JSON字符串({\"videoId\":0,\"videoName\":\"第三个视频\",\"introduction\":\"简介\",\"copyright\":\"版权\"})只支持单个",required=true)@RequestParam(value="videoJsonStr",required=true)String videoJsonStr,
			@ApiParam(value="字幕JSON字符串(示例：[{\"videoId\":0,\"type\":\"1\",\"address\":\"1\"},{\"videoId\":0,\"type\":\"1\",\"address\":\"1\"}])",required=false)
			@RequestParam(value="jsonStr",required=false) String jsonStr){
		
		return this.videoService.syncVideo(cloudAddress,seed,authToken,videoJsonStr,jsonStr);
		
	}
	
	@RequestMapping(value = "/deleteVideo", method = RequestMethod.DELETE)
	@ApiOperation(value = "批量删除视频")
	public Map<String, Object> deleteVideo(
			@ApiParam(value = "视频ID字符串(示例：a,b,c)", required = true) @RequestParam("videoIds") String videoIds) {
		return videoService.deleteVideo(videoIds);
	}
	
	@RequestMapping(value="/updateVideo",method=RequestMethod.POST)
	@ApiOperation(value="更新视频")
	public Map<String,Object> updateVideo(
			@ApiParam(value="视频对象JSON字符串({\"videoId\":0,\"videoName\":\"第三个视频\",\"introduction\":\"简介\",\"copyright\":\"版权\"})只支持单个",required=true)
			@RequestParam(value="videoJsonStr",required=true)String videoJsonStr){
		return videoService.updateVideo(videoJsonStr);
	}
	
	@RequestMapping(value="/getList",method=RequestMethod.GET)
	@ApiOperation(value="视频列表")
	public Map<String,Object> getList(
			@ApiParam(value="视频名称",required=false)@RequestParam(value="videoName",required=false)String videoName,
			@ApiParam(value="状态",required=false)@RequestParam(value="status",required=false)Integer status,
			@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
		}
		return videoService.getList(videoName,status,co,userId);
	}
	
}
